06. Exercise: Defining a Functional Interface
Binary Operations
In this exercise, you will define a functional interface that represents a binary operation between two integers.
Binary operation is a fancy term that refers to a method that takes two arguments.
In this case, your functional method should take two int
arguments, and it should return an int
result.
- First, create an interface called
BinaryOperation
and put it in its own file. The interface should contain one abstract method,apply()
, which takes twoint
arguments and returns anint
.
Congratulations, you just created your first Functional Interface!
- Next, annotate the interface with
@FunctionalInterface
. If you structured the functional interface correctly, the Java compiler will allow it. - Then, make sure
Main.java
compiles:
javac Main.java
If you chose a name other than "BinaryOperation" for the functional interface, you will have to edit the code in the main()
method to refer to your functional interface.
- Finally, run the program:
java -ea Main
Note: The -ea
flag stands for "enable assertions". It's useful here because Main.java
has an assert
statement in it.
TODO List
Task Feedback:
Nice work! You successfully created a custom functional interface!
Code
If you need a code on the https://github.com/udacity.
export PATH=/data/jdk-15.0.1/bin:$PATH
export JAVA_HOME=/data/jdk-15.0.1/bin